package com.itheima.stock;

import com.itheima.stock.config.CommonConfig;
import com.itheima.stock.mapper.SysUserMapper;
import com.itheima.stock.pojo.SysUser;
import com.itheima.stock.service.StockTimerService;
import lombok.Data;
import org.checkerframework.checker.units.qual.A;
import org.checkerframework.checker.units.qual.Acceleration;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestComponent;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.*;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.net.URL;
import java.util.List;
import java.util.Map;


@SpringBootTest
public class TaetAll {
    @Autowired
    private StockTimerService stockTimerService;

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private CommonConfig config;
    @Autowired
    private RestTemplate restTemplate;
    @Test
    public void taet() {
        SysUser user = sysUserMapper.selectByPrimaryKey(1246368763562037248l);
        System.out.println("user = " + user);
    }

    @Test
    public void taet1() {
     String xx="changweixin";
        PasswordEncoder encoder = config.passwordEncoder();
        String encode = encoder.encode(xx);
        boolean matches = encoder.matches(xx, "$2a$10$HkczoF5E3UnVdlYXfQ50qupIbe1gX3/euUx11UeX3oos.yi6JaAaq");
        System.out.println(matches);
    }
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Test
    public void taeti(){
    redisTemplate.opsForValue().set("chang","xinge");
        String chang = redisTemplate.opsForValue().get("chang");
        System.out.println(chang);
    }
    @Test
    public void test01(){
        String url="http://localhost:6666/account/getByUserNameAndAddress?userName=itheima&address=shanghai";
        /*
          参数1：url请求地址
          参数2：请求返回的数据类型
         */
        //获取响应头
        Account forObject = restTemplate.getForObject(url, Account.class);
        System.out.println(forObject);

    }

    @Test
    public void test03(){
        String url="http://localhost:6666/account/getHeader";
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("userName","zhangsan");
        HttpEntity<Map> entity = new HttpEntity<>(httpHeaders);
        ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
        String body = exchange.getBody();
        System.out.println(body);
     }
    @Test
    public void test04(){
        String url="http://localhost:6666/account/addAccount";
        //设置请求头，指定请求数据方式
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-type","application/x-www-form-urlencoded");
        //组装模拟form表单提交数据
        LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("id","10");
        map.add("userName","itheima");
        map.add("address","shanghai");
        HttpEntity<LinkedMultiValueMap<String, Object>> httpEntity = new HttpEntity<>(map, headers);
        /*
            参数1：请求url地址
            参数2：请求方式 POST
            参数3：请求体对象，携带了请求头和请求体相关的参数
            参数4：响应数据类型
         */
        ResponseEntity<Account> exchange = restTemplate.exchange(url, HttpMethod.POST, httpEntity, Account.class);
        Account body = exchange.getBody();
        System.out.println(body);
    }
    @Test
    public void test05() {
        String url = "http://localhost:6666/account/updateAccount";
        //设置请求头的请求参数类型
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-type","application/json; charset=utf-8");
        String jsonReq="{\"address\":\"上海\",\"id\":\"1\",\"userName\":\"zhangsan\"}";
        //构建请求对象
        HttpEntity<String> httpEntity = new HttpEntity<>(jsonReq, headers);
        ResponseEntity<Account> responseEntity = restTemplate.exchange(url, HttpMethod.POST, httpEntity, Account.class);
        Account body = responseEntity.getBody();
        System.out.println(body);
    }
    @Test
    public void test06(){
        String url="http://localhost:6666/account/getCookie";
        ResponseEntity<String> result = restTemplate.getForEntity(url, String.class);
        //获取cookie
        List<String> cookies = result.getHeaders().get("Set-Cookie");
        //获取响应数据
        String resStr = result.getBody();
        System.out.println(resStr);
        System.out.println(cookies);
    }
    @Data
    public static class Account{
        private Integer id;
        private  String userName;
        private  String address;
    }
    @Test
    public void test07(){
        stockTimerService.getInnerMarketInfo();
    }
    @Test
    public void tele9(){
        stockTimerService.getStockRtIndex();
    }
    @Test
    public void test10(){
        stockTimerService.getStockSectorRtIndex();
    }

    }

